package com.qqtech.qquan.hi.service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.hi.dao.HiRuleDao;
import com.qqtech.qquan.hi.enums.HiRuleClassifyDictEnum;
import com.qqtech.qquan.hi.model.HiRule;
import com.qqtech.qquan.hi.model.HiRuleResultVo;
import com.qqtech.qquan.user.service.UserBaseService;

@Service
public class HiCommunityServiceImpl extends BaseServiceImpl<HiRule> implements HiCommunityService {
	public static final Logger log = LoggerFactory.getLogger(HiCommunityServiceImpl.class);

	@Resource
	private HiRuleDao hiRuleDao;

	@Override
	protected BaseDao<HiRule> getBaseDao() {
		return hiRuleDao;
	}

	@Resource
	private HiRuleService hiRuleService;

	@Resource
	private HiRuleResultService hiRuleResultService;

	@Resource
	private UserBaseService userBaseService;

	/**
	 * 判断是否为今天
	 * 
	 * @param updateTime
	 *            输入的时间
	 * @return
	 */
	private boolean isToday(Timestamp updateTime) {
		Calendar cd = Calendar.getInstance();
		Date date1 = cd.getTime();
		Date date2 = updateTime;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (sdf.format(date1).equals(sdf.format(date2))) {
			return true;
		}
		return false;
	}

	/**
	 * 判断是否为昨天
	 * 
	 * @param updateTime
	 *            输入的时间
	 * @return
	 */
	private boolean isLastDay(Timestamp updateTime) {
		Calendar cd = Calendar.getInstance();
		cd.add(Calendar.DATE, -1);
		Date date1 = cd.getTime();
		Date date2 = updateTime;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (sdf.format(date1).equals(sdf.format(date2))) {
			return true;
		}
		return false;
	}

	@Override
	public void sign(Integer userId, Integer communityId) {
		if (userId != null && communityId != null) {
			Integer times = null;
			Integer classify = HiRuleClassifyDictEnum.SIGN.getCode();
			// 是否有签到规则
			boolean hasRule = hiRuleService.checkHaveRuleByClassify(classify, YesNoEnum.YES.getCode());
			if (hasRule) {
				HiRuleResultVo hiRuleResultNewest = hiRuleResultService.getNewest(userId, classify, communityId);
				if (hiRuleResultNewest == null) { // 没有数据，表示首次签到
					times = 1;
				} else {
					if (isLastDay(hiRuleResultNewest.getUpdAt())) {// 上次记录是昨天
						times = hiRuleResultNewest.getTimes() + 1;
					} else if (isToday(hiRuleResultNewest.getUpdAt())) { // 上次记录是今天
						// 不处理
						times = null;
					} else {
						// 时间间断了
						times = 1;
						hiRuleResultNewest.setTimes(0);
					}
				}
				if (times != null) {
					hiRuleResultService.handleSave(userId, classify, communityId, times, hiRuleResultNewest);
				}
			}
		}
	}

	@Override
	public void join(Integer userId, Integer communityId) {
		if (userId != null && communityId != null) {
			Integer classify = HiRuleClassifyDictEnum.JOIN_COMMUNITY.getCode();
			// 是否有加入社区规则
			boolean hasRule = hiRuleService.checkHaveRuleByClassify(classify, YesNoEnum.YES.getCode());
			if (hasRule) {
				HiRuleResultVo hiRuleResultNewest = hiRuleResultService.getNewest(userId, classify, null);
				if (hiRuleResultNewest == null) { // 没有数据，表示首次
					hiRuleResultService.handleSave(userId, classify, communityId, 1, null);
				}
			}
		}
	}

	@Override
	public void addMessage(Integer userId, Integer communityId) {
		if (userId != null) {
			Integer classify = HiRuleClassifyDictEnum.NEIGHBOURHOOD_DYNAMIC.getCode();
			// 是否有发布邻里动态规则
			boolean hasRule = hiRuleService.checkHaveRuleByClassify(classify, YesNoEnum.YES.getCode());
			if (hasRule) {
				Integer times = null;
				HiRuleResultVo hiRuleResultNewest = hiRuleResultService.getNewest(userId, classify, null);
				if (hiRuleResultNewest == null) {
					times = 1;
				} else {
					if (isToday(hiRuleResultNewest.getUpdAt())) { // 上次记录是今天
						times = hiRuleResultNewest.getTimes() + 1;
					} else {
						times = 1;
						hiRuleResultNewest.setTimes(0);
					}
				}
				hiRuleResultService.handleSave(userId, classify, communityId, times, hiRuleResultNewest);
			}
		}
	}

	@Override
	public void commentMessage(Integer userId, Integer messageId) {
		if (userId != null && messageId != null) {
			Integer classify = HiRuleClassifyDictEnum.COMMUNITY_DYNAMIC_COMMENT.getCode();
			// 是否有发布圈子动态送荣誉值的规则
			boolean hasRule = hiRuleService.checkHaveRuleByClassify(classify, YesNoEnum.YES.getCode());
			if (hasRule) {
				HiRuleResultVo hiRuleResultNewest = hiRuleResultService.getNewest(userId, classify, messageId);
				if (hiRuleResultNewest == null) { // 不等于空则表示给过积分了
					hiRuleResultService.handleSave(userId, classify, messageId, 1, hiRuleResultNewest);
				}
			}
		}
	}

	@Override
	public void praiseMessage(Integer userId, Integer messageId) {
		if (userId != null && messageId != null) {
			Integer classify = HiRuleClassifyDictEnum.COMMUNITY_DYNAMIC_PRAISE.getCode();
			// 是否有规则
			boolean hasRule = hiRuleService.checkHaveRuleByClassify(classify, YesNoEnum.YES.getCode());
			if (hasRule) {
				HiRuleResultVo hiRuleResultNewest = hiRuleResultService.getNewest(userId, classify, messageId);
				if (hiRuleResultNewest == null) { // 不等于空则表示给过积分了
					hiRuleResultService.handleSave(userId, classify, messageId, 1, hiRuleResultNewest);
				}
			}
		}
	}

	@Override
	public int getSignTimes(Integer userId, Integer communityId) {
		int times = 1;
		HiRuleResultVo hiRuleResultNewest = hiRuleResultService.getNewest(userId, HiRuleClassifyDictEnum.SIGN.getCode(),
				communityId);
		if (hiRuleResultNewest == null) { // 没有数据，表示首次签到
			times = 1;
		} else {
			if (isLastDay(hiRuleResultNewest.getUpdAt())) {// 上次记录是昨天
				times = hiRuleResultNewest.getTimes() + 1;
			} else if (isToday(hiRuleResultNewest.getUpdAt())) { // 上次记录是今天
				times = hiRuleResultNewest.getTimes();
			} else {
				// 时间间断了
				times = 1;
			}
		}
		return times;
	}

}